package com.isoftstone.securityframework.device.domain;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.Callable;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.ExecutionException;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;

import com.isoftstone.securityframework.api.Account;
import com.isoftstone.securityframework.api.Permission;
import com.isoftstone.securityframework.api.util.AuthUtil;
import com.isoftstone.securityframework.device.exception.NullDeviceException;
import com.isoftstone.securityframework.device.subject.DeviceSubject;
import com.isoftstone.securityframework.device.subject.DeviceToken;

/**
 * 设备
 * @author david
 *
 */
public class Device implements com.isoftstone.securityframework.device.Device,DeviceSubject{
	
	private Integer id;
	@SuppressWarnings("unused")
	private String platformId;
	private String deviceFlag;
	private Integer status;
	
	private String createDate;
	private String lastLoginTime;

	private List<DeviceExt> deivceExts;
	private List<Account> accounts;
	private List<Permission> permissions;
	private List<DeviceGroup> groups;
	
	
	
	
	public Device(String deviceFlag) {
		super();
		this.deviceFlag = deviceFlag;
	}
	public Device() {
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getDeviceFlag() {
		return deviceFlag;
	}
	public void setDeviceFlag(String deviceFlag) {
		this.deviceFlag = deviceFlag;
	}
	

	@Override
	public Integer getStatus() {
		return this.status;
	}
	public void setStatus(Integer status){
		this.status = status;
	}
	
	public String getCreateDate() {
		return createDate;
	}
	public void setCreateDate(String createDate) {
		this.createDate = createDate;
	}
	public String getLastLoginTime() {
		return lastLoginTime;
	}
	public void setLastLoginTime(String lastLoginTime) {
		this.lastLoginTime = lastLoginTime;
	}
	public List<Account> getAccounts() {
		return accounts;
	}
	public void setAccounts(List<Account> accounts) {
		this.accounts = accounts;
	}
	public List<Permission> getPermissions() {
		return permissions;
	}
	public void setPermissions(List<Permission> permissions) {
		this.permissions = permissions;
	}
	public List<DeviceGroup> getGroups() {
		return groups;
	}
	public void setGroups(List<DeviceGroup> groups) {
		this.groups = groups;
	}
	public String getPlatformId() {
		return AuthUtil.getPlatformId();
	}
	public List<DeviceExt> getDeivceExts() {
		return deivceExts;
	}
	public void setDeivceExts(List<DeviceExt> deivceExts) {
		this.deivceExts = deivceExts;
	}
	@Override
	public com.isoftstone.securityframework.device.Device controllable() {
		return null;
	}
	@Override
	public com.isoftstone.securityframework.device.Device controlled() {
		return null;
	}
	
	private Subject getDelegate() {
		return SecurityUtils.getSubject();
	}
	@Override
	public Object getPrincipal() {
		return getDelegate().getPrincipal();
	}
	@Override
	public PrincipalCollection getPrincipals() {
		return getDelegate().getPrincipals();
	}
	@Override
	public boolean isPermitted(String permission) {
		return getDelegate().isPermitted(permission);
	}
	@Override
	public boolean isPermitted(org.apache.shiro.authz.Permission permission) {
		return getDelegate().isPermitted(permission);
	}
	@Override
	public boolean[] isPermitted(String... permissions) {
		return getDelegate().isPermitted(permissions);
	}
	@Override
	public boolean[] isPermitted(
			List<org.apache.shiro.authz.Permission> permissions) {
		return getDelegate().isPermitted(permissions);
	}
	@Override
	public boolean isPermittedAll(String... permissions) {
		return getDelegate().isPermittedAll(permissions);
	}
	@Override
	public boolean isPermittedAll(
			Collection<org.apache.shiro.authz.Permission> permissions) {
		return getDelegate().isPermittedAll(permissions);
	}
	@Override
	public void checkPermission(String permission)
			throws AuthorizationException {
		getDelegate().checkPermission(permission);
	}
	@Override
	public void checkPermission(org.apache.shiro.authz.Permission permission)
			throws AuthorizationException {
		getDelegate().checkPermission(permission);
	}
	@Override
	public void checkPermissions(String... permissions)
			throws AuthorizationException {
		getDelegate().checkPermissions(permissions);
	}
	@Override
	public void checkPermissions(
			Collection<org.apache.shiro.authz.Permission> permissions)
			throws AuthorizationException {
		getDelegate().checkPermissions(permissions);
	}
	@Override
	public boolean hasRole(String roleIdentifier) {
		return getDelegate().hasRole(roleIdentifier);
	}
	@Override
	public boolean[] hasRoles(List<String> roleIdentifiers) {
		return getDelegate().hasRoles(roleIdentifiers);
	}
	@Override
	public boolean hasAllRoles(Collection<String> roleIdentifiers) {
		return getDelegate().hasAllRoles(roleIdentifiers);
	}
	@Override
	public void checkRole(String roleIdentifier) throws AuthorizationException {
		getDelegate().checkRole(roleIdentifier);
	}
	@Override
	public void checkRoles(Collection<String> roleIdentifiers)
			throws AuthorizationException {
		getDelegate().checkRoles(roleIdentifiers);
	}
	@Override
	public void checkRoles(String... roleIdentifiers)
			throws AuthorizationException {
		getDelegate().checkRoles(roleIdentifiers);
	}
	

	@Override
	public void login() {
		DeviceToken token = new DeviceToken(this.deviceFlag);
		this.login(token);
	}
	@Override
	public void login(String deviceFlag) {
		this.deviceFlag = deviceFlag;
		DeviceToken token = new DeviceToken(this.deviceFlag);
		this.login(token);
	}
	
	
	@Override
	public void login(AuthenticationToken token) throws AuthenticationException {
		if(token.getPrincipal()==null){
			throw new NullDeviceException();
		}
		getDelegate().login(token);
	}
	@Override
	public boolean isAuthenticated() {
		return getDelegate().isAuthenticated();
	}
	@Override
	public boolean isRemembered() {
		return getDelegate().isRemembered();
	}
	@Override
	public Session getSession() {
		return getDelegate().getSession();
	}
	@Override
	public Session getSession(boolean create) {
		return getDelegate().getSession(create);
	}
	@Override
	public void logout() {
		getDelegate().logout();
	}
	@Override
	public <V> V execute(Callable<V> callable) throws ExecutionException {
		return getDelegate().execute(callable);
	}
	@Override
	public void execute(Runnable runnable) {
		getDelegate().execute(runnable);
	}
	@Override
	public <V> Callable<V> associateWith(Callable<V> callable) {
		return getDelegate().associateWith(callable);
	}
	@Override
	public Runnable associateWith(Runnable runnable) {
		return getDelegate().associateWith(runnable);
	}
	@Override
	public void runAs(PrincipalCollection principals)
			throws NullPointerException, IllegalStateException {
		getDelegate().runAs(principals);
	}
	@Override
	public boolean isRunAs() {
		return getDelegate().isRunAs();
	}
	@Override
	public PrincipalCollection getPreviousPrincipals() {
		return getDelegate().getPreviousPrincipals();
	}
	@Override
	public PrincipalCollection releaseRunAs() {
		return getDelegate().releaseRunAs();
	}
}
